﻿using System;
using HttpApiGateway.Api;
using HttpApiGateway.FileUp.Interface;
using HttpApiGateway.Interface;

using RpcClient.Interface;
using RpcHelper;

namespace HttpApiGateway.Collect
{
    internal class UnityCollect
    {
        private static readonly IUnityCollect _Unity = RpcClient.RpcClient.Unity;
        private static Type _apiBasicType = typeof(ApiController);
        public static bool RegisterApi(Type type)
        {
            if (type == _apiBasicType)
            {

            }
            if (type.BaseType==PublicDataDic.ObjectType)
            {
                return false;
            }
            Type apiType = type.GetInterface(PublicDict.IApiType.FullName);
            if (apiType == null)
            {
                return false;
            }
            return _Unity.Register(PublicDict.IGatewayType, type, type.FullName);
        }
        public static bool RegisterUpBlockFileApi(Type type)
        {
            if (type.BaseType == PublicDataDic.ObjectType)
            {
                return false;
            }
            Type apiType = type.GetInterface(PublicDict.IUpBlockFileType.FullName);
            if (apiType == null)
            {
                return false;
            }
            return _Unity.Register(PublicDict.IUpBlockFileType, type, type.FullName);
        }
        public static bool AddApi(Type type)
        {
            Type apiType = type.GetInterface(PublicDict.IApiType.FullName);
            if (apiType == null)
            {
                return false;
            }
            else if (_Unity.IsRegistered(type, type.FullName))
            {
                return true;
            }
            return _Unity.Register(PublicDict.IGatewayType, type, type.FullName);
        }
        public static bool AddGateway(Type type)
        {
            Type apiType = type.GetInterface(PublicDict.IGatewayType.FullName);
            if (apiType == null)
            {
                return false;
            }
            else if (_Unity.IsRegistered(type, type.FullName))
            {
                return true;
            }
            return _Unity.Register(PublicDict.IGatewayType, type, type.FullName);
        }
        public static bool RegisterGateway(Type type)
        {
            if (type == _apiBasicType)
            {
                return false;
            }
            Type apiType = type.GetInterface(PublicDict.IGatewayType.FullName);
            if (apiType == null)
            {
                return false;
            }
            return _Unity.Register(PublicDict.IGatewayType, type, type.FullName);
        }
        public static IApiController GetApi(Type type)
        {
            return (IApiController)_Unity.Resolve(PublicDict.IGatewayType, type.FullName);
        }
        public static IApiGateway GetGateway(Type type)
        {
            return (IApiGateway)_Unity.Resolve(PublicDict.IGatewayType, type.FullName);
        }
        public static IUpBlockFile GetUpTask(string name)
        {
            return _Unity.Resolve<IUpBlockFile>(name);
        }
    }
}
